<template>
  <div>
    <el-dialog title="读取平面图" :visible.sync="addEditIsDialog_c" :close-on-click-modal="false" @close="resetForm('formName')" width="800px">
      <el-form :model="ruleForm" :rules="rules" ref="formName" label-width="120px" class="demo-ruleForm" :class="{ 'form-rules-show': seeType }">
        <el-row>
          <el-col :span="24">
            <el-form-item label="考场名称：" prop="examinationRoomId">
              <el-select v-model="ruleForm.examinationRoomId" placeholder="请选择考场名称" clearable @change="schoolChange" :disabled="seeType || userType != 0">
                <el-option v-for="item in schoolArray" :key="item.id" :label="item.name" :value="item.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="巡检设备：" prop="id">
              <el-select v-model="ruleForm.id" placeholder="请选择巡检设备" clearable>
                <el-option v-for="item in deviceArray" :key="item.id" :label="item.name" :value="item.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="resetForm('formName')">取 消</el-button>
        <el-button type="primary" @click="submitForm('formName')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script type="text/ecmascript-6">
import { readInspectionImage, getInspectionDevices } from '@/api/inspection'
import { getSchoolInfos } from '@/api/school'

export default {
  components: {},
  props: {
    addEditId: {
      type: String,
      default: ''
    },
    addEditStatus: {
      type: String,
      default: 'add'
    },
    addEditIsDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      ruleForm: {
        // 表单字段
        id: '',
        examinationRoomId: ''
      },
      rules: {
        // 表单规则
        examinationRoomId: [{ required: true, message: '请选择考场组织', trigger: 'change' }],
        id: [{ required: true, message: '请选择巡检设备', trigger: 'change' }]
      },
      addEditIsDialog_c: false, // 弹窗是否显示
      seeType: false, // 是否禁用
      treeId: [], // 考场组织id集合
      schoolArray: [], // 考场组织集合
      deviceArray: [] // 巡检设备集合
    }
  },
  watch: {
    addEditIsDialog(val) {
      this.addEditIsDialog_c = val
      if (this.addEditIsDialog_c) {
        // 获取考场组织
        this.getSchoolInfos()
      }
    }
  },
  methods: {
    // 提交
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          readInspectionImage(this.ruleForm).then((res) => {
            if (res.code == 200) {
              this.$message({
                type: 'success',
                message: res.msg
              })

              // 关闭弹窗
              this.$parent.closeAddEdit()
              // 刷新列表
              this.$parent.onSubmit()
            }
          })
        }
      })
    },
    // 取消
    resetForm(formName) {
      this.$refs[formName].resetFields()
      this.treeId = []
      this.$parent.closeAddEdit()
    },
    // 获取考场组织
    getSchoolInfos() {
      getSchoolInfos().then((res) => {
        if (res.code == 200) {
          this.schoolArray = res.data
        }
      })
    },
    // 考场组织下拉切换
    changeTreeItem(val) {
      let len = val.length
      if (val && len == 5) {
        this.ruleForm.examinationRoomId = val[len - 1]
      } else {
        this.ruleForm.examinationRoomId = ''
        this.treeId = []
      }

      // 清空
      this.ruleForm.id = ''
      this.deviceArray = []

      // 获取巡检设备列表
      this.getInspectionDeviceList()
    },
    // 获取巡检设备列表
    getInspectionDeviceList() {
      if (!this.ruleForm.examinationRoomId || this.ruleForm.examinationRoomId == '') {
        return
      }
      getInspectionDevices({ id: this.ruleForm.examinationRoomId }).then((res) => {
        if (res.code == 200) {
          this.deviceArray = res.data
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
.demo-ruleForm {
  width: 95%;
  margin: 0 auto;
  padding-top: 10px;

  .el-input-number {
    width: 100%;
  }

  .el-cascader {
    width: 100%;
  }
}
</style>
